/**   
 * @Title: UserModel.java 
 * @Package org.playgod1984.du.auth.model 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author playgod playgod1984@163.com  
 * @date 2014年10月15日 下午1:10:58 
 * @version V1.0   
 */
package org.playgod1984.du.auth.model;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;

import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import freemarker.template.utility.StringUtil;
/**
 mysql> desc dud_auth_user;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| loginaccount | varchar(50)  | NO   | PRI | NULL    |       |
| password     | varchar(100) | YES  |     | NULL    |       |
| displayname  | varchar(20)  | YES  |     | NULL    |       |
| sex          | varchar(1)   | YES  |     | NULL    |       |
| remark       | varchar(200) | YES  |     | NULL    |       |
| creator      | varchar(50)  | YES  |     | NULL    |       |
| createtime   | date         | YES  |     | NULL    |       |
| updator      | varchar(20)  | YES  |     | NULL    |       |
| updatetime   | date         | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
 */
/** 
 * @ClassName: UserModel 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author playgod playgod1984@163.com 
 * @date 2014年10月15日 下午1:10:58 
 *  
 */
@SuppressWarnings("all")
public class User extends Model<User> {
	private static final Logger log = Log4jLogger.getLogger(User.class);
	public static final User dao = new User();
	
	/**
	 * @Title: getUsersWithPagenate
	 * @author:playgod playgod1984@163.com 
	 * @Description: Query Users with pagenate 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Page<UserModel>    返回类型 
	 * @throws
	 */
	public Page<User> getUsersWithPagenate(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sql_where=" where 1=1 ";
		if(StringUtils.isNotBlank((String)params.get("xm"))){
			sql_where+=" and displayname like '%"+params.get("xm")+"%'";
			sql_where+=" or loginaccount like '%"+params.get("xm")+"%'";
		}
		return paginate(pageNumber, pageSize, "select *", "from dud_auth_user" + sql_where);
	}
	/**
	 * 
	 * @Title: deleteRows
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int deleteRows(Map params){
		int results =0;
		try{
			String[] ids = StringUtils.split((String)params.get("keys"),",");
			for(int i=0;i<ids.length;i++){
				if(dao.deleteById(ids[i])){
					results++;
				}
			}
			return results;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return -1;
		}
	}
	/**
	 * @Title: save
	 * @author:playgod playgod1984@163.com 
	 * @Description: save
	 * @param @param params
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public boolean save(Map params){
		Record r = new Record();
		r.set("loginaccount", params.get("loginaccount"));
		r.set("password", params.get("loginaccount"));
		r.set("displayname", params.get("displayname"));
		r.set("sex", params.get("sex"));
		r.set("remark", params.get("remark"));
		r.set("updator", params.get("currentUser"));
		r.set("updatetime",new Date());
		try{
			if("A".equals((String)params.get("screen"))){//ADD
				r.set("creator", params.get("currentUser"));
				r.set("createtime",new Date());
				Db.save("dud_auth_user", r);
			}else{
				Db.update("dud_auth_user", "loginaccount", r);
			}
			return true;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return false;
		}
	}
}
